<?php
/**
 * @title 二分排序
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution{

    public function find($nums = []){
        /**
         * @since 2020-09-07 升序结果集
         */
        $list = [];

        /**
         * @since 2020-09-07 迭代
         */
        foreach ($nums as $num){
            $left = 0;
            $right = count($list)-1;

            /**
             * @since 2020-09-07 空元素
             */
            if($right == -1){
                $list[] = $num;
            } else {
                /**
                 * @since 2020-09-07 二分排序
                 */
                while($left<=$right){
                    $midKey = ($left+$right)>>1;
                    $midVal = $list[$midKey];

                    if($num > $midVal){
                        $left = $midKey+1;
                    } else {
                        $right = $midKey-1;
                    }
                }

                /**
                 * @since 2020-09-07 left位置插入
                 */
                array_splice($list, $left, 0, $num);
            }
        }

        /**
         * @return
         */
        return $list;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.07.php
 */
$datas = [
    [1,2,3],
    [2,1,3],
    [3,1,2],
    [9,4,6,2,3,7,1,0,8,5],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->find($data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
